﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using DataAccess.AccountManage;
using DataAccess.BikeManage.logical;
using Ninject;
using WinForm.Helper;

namespace WinForm.UserManage
{
    public partial class FrmUnlock : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IOperate _operate = Kernel.Get<IOperate>();

        public FrmUnlock()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 查询用户信息，包括扣款信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveIDInfo_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                var zq58Logical = new Zq58Logical();
                ResultInfo<CpuAccount> cpuAccount = zq58Logical.ReadAccountInfo();
                if (cpuAccount.ResultNumber == ResultEnum.Success)
                {
                    ResultInfo<Decimal> totalMoney = zq58Logical.QueryPurse();
                    if (totalMoney.ResultNumber == ResultEnum.Success)
                    {
                        //初始化用户信息显示
                        lbName.Text = cpuAccount.Obj.DisplayName;
                        lbSex.Text = cpuAccount.Obj.Sex;
                        lbCpuVersion.Text = cpuAccount.Obj.CpuCardVersion;
                        lbStatus.Text = cpuAccount.Obj.CpuCardStatus;
                        lbCpuNo.Text = cpuAccount.Obj.CpuCardNo;
                        lbCurrentMoney.Text = totalMoney.Obj.ToString(CultureInfo.InvariantCulture);

                        ResultInfo<BorrowBike> borrowBike = zq58Logical.ReadBorrowBikeInfo();
                        if (borrowBike.ResultNumber == ResultEnum.Success)
                        {
                            //将开户日期和结束日期先存着
                            lbOpenDate.Text = borrowBike.Obj.OpenAccountDate;
                            lbValideDate.Text = borrowBike.Obj.EndAccountDate;

                            ResultInfo<Dictionary<string, Decimal>> costmoney = _operate.GetBorrowBikeCost(cpuAccount.Obj.CpuCardId, borrowBike.Obj.BorrowBikeid, Converts.ConvertStringToDateTime(borrowBike.Obj.BorrowTime));
                            if (costmoney.ResultNumber == ResultEnum.Success)
                            {
                                lbCostMoney.Text = costmoney.Obj["cost"].ToString(CultureInfo.InvariantCulture);
                                lbConsumeId.Text = Converts.ConvertToInt(costmoney.Obj["consumeid"].ToString()).ToString();

                                tabItem1.Visible = false;
                                tabItem2.Visible = true;
                            }
                            else
                            {
                                errorMessage = errorMessage + costmoney.Describe;
                            }
                        }
                        else
                        {
                            errorMessage = errorMessage + borrowBike.Describe;
                        }
                    }
                    else
                    {
                        errorMessage = errorMessage + totalMoney.Describe;
                    }
                }
                else
                {
                    errorMessage = cpuAccount.Describe;
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        /// 解锁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                var zq58Logical = new Zq58Logical();
                ResultInfo<CpuAccount> cpuAccount = zq58Logical.ReadAccountInfo();
                if (cpuAccount.ResultNumber == ResultEnum.Success)
                {
                    Decimal costMoney = Converts.ConvertToDecimal(lbCostMoney.Text);
                    Decimal balanceMoney = Converts.ConvertToDecimal(lbCurrentMoney.Text);
                    if (costMoney > 0 && costMoney < balanceMoney)
                    {
                        var costInDb = UnLockCard(costMoney, cpuAccount);
                        if (costInDb.ResultNumber == ResultEnum.Success && costInDb.Obj)
                        {
                            ResultInfo<byte[]> cpucost = zq58Logical.CostMoney(Converts.ConvertToDecimal(lbCostMoney.Text));

                            if (cpucost.ResultNumber == ResultEnum.Success)
                            {
                                ResultInfo<byte[]> fresheBorrowBike = zq58Logical.WriteBorrowBikeInfo(new BorrowBike { OpenAccountDate = lbOpenDate.Text, EndAccountDate = lbValideDate.Text });
                                if (fresheBorrowBike.ResultNumber == ResultEnum.Success)
                                {
                                    ResultInfo<Decimal> newCurrentMoney = zq58Logical.QueryPurse();
                                    if (newCurrentMoney.ResultNumber == ResultEnum.Success)
                                    {
                                        MyMessageBox.ShowMessageBoxInfo(string.Format("本次解锁成功！\n原余额为：{0}，扣款：{1}，当前余额为：{2}",
                                                                      lbCurrentMoney.Text, lbCostMoney.Text,
                                                                      newCurrentMoney.Obj));
                                        this.Close();
                                        this.Dispose();
                                    }
                                    else
                                    {
                                        errorMessage = errorMessage + newCurrentMoney.Describe;
                                    }
                                }
                                else
                                {
                                    errorMessage = errorMessage + fresheBorrowBike.Describe;
                                }
                            }
                            else
                            {
                                errorMessage = errorMessage + cpucost.Describe;
                            }
                        }
                        else
                        {
                            errorMessage = errorMessage + costInDb.Describe;
                        }
                    }
                    else
                    {
                        errorMessage = errorMessage + "余额不足，请先充值";
                    }
                }
                else
                {
                    errorMessage = cpuAccount.Describe;
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        /// 解锁用户卡
        /// </summary>
        /// <param name="costMoney"></param>
        /// <param name="cpuAccount"></param>
        /// <returns></returns>
        private ResultInfo<bool> UnLockCard(decimal costMoney, ResultInfo<CpuAccount> cpuAccount)
        {
            ResultInfo<bool> costInDb = _operate.UnlockCpuCard(costMoney, cpuAccount.Obj.CpuCardId,
                Converts.ConvertToInt(lbConsumeId.Text), SystemInfo.OperateAddress, SystemInfo.OperateAddressId,
                SystemInfo.OperatePsamId, SystemInfo.OperateId);
            return costInDb;
        }
    }
}
